//
//  clientSearchViewController.m
//  LazyCat
//
//  Created by rocky on 16/1/4.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "clientSearchViewController.h"
#import "detailClientViewController.h"
#import "JsWebviewNativeManager.h"
#import "BusinessSchollController.h"

@interface clientSearchViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UITextField *textField;//
@property (nonatomic,strong) NSArray *searchArr;//搜索数组
@property (nonatomic,strong) UITableView *tableV;//表视图
@property (nonatomic,strong) NSString *keyWord;//关键字

@end

@implementation clientSearchViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([self.isFrom isEqualToString:@"khgl"]) {
       self.searchArr = [NSUserDefualManager getValueByKey:@"khgl"];
    }else if ([self.isFrom isEqualToString:@"sxy"]) {
        self.searchArr = [NSUserDefualManager getValueByKey:@"sxy"];
    }else if ([self.isFrom isEqualToString:@"kd"]) {
        self.searchArr = [NSUserDefualManager getValueByKey:@"kd"];
    }
    if (self.searchArr.count == 0) {
        self.tableV.hidden = YES;
    }else {
        self.tableV.hidden = NO;
    }
    [self.tableV reloadData];
}
-(void)viewDidAppear:(BOOL)animated {
    [self.textField becomeFirstResponder];//键盘弹起
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"" doBack:NO];
    [self initTopView];//创建头部搜索
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStyleGrouped];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidKeyBoard:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
}
-(void)hidKeyBoard:(UITapGestureRecognizer *)tap {
    [self.view resignFirstResponder];
    [self.textField resignFirstResponder];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.searchArr.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    }
    if (indexPath.row == self.searchArr.count) {
        cell.textLabel.frame = CGRectMake(0, 0, ScreenWidth, 44);
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.text = @"清除搜索历史";
        cell.textLabel.font = [UIFont systemFontOfSize:16];
    }else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@",self.searchArr[indexPath.row]];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.isFrom isEqualToString:@"khgl"]) {
        if (indexPath.row == self.searchArr.count) {//点击删除
            [NSUserDefualManager storageValue:@[] forKey:@"khgl"];
            self.searchArr = [NSUserDefualManager getValueByKey:@"khgl"];
            self.tableV.hidden = YES;
            [self.tableV reloadData];
        }else {
            self.keyWord = self.searchArr[indexPath.row];
            detailClientViewController *dCVC = [[detailClientViewController alloc]init];
            dCVC.key = self.keyWord;
            dCVC.isFrom = @"0";
            [self.navigationController pushViewController:dCVC animated:YES];
        }
    }else if ([self.isFrom isEqualToString:@"sxy"]) {
        if (indexPath.row == self.searchArr.count) {//点击删除
            [NSUserDefualManager storageValue:@[] forKey:@"sxy"];
            self.searchArr = [NSUserDefualManager getValueByKey:@"sxy"];
            self.tableV.hidden = YES;
            [self.tableV reloadData];
        }else {
            self.keyWord = self.searchArr[indexPath.row];
            BusinessSchollController *busScVC = [[BusinessSchollController alloc]init];
            busScVC.keyword = self.keyWord;
            [self.navigationController pushViewController:busScVC animated:YES];
        }
    }else if ([self.isFrom isEqualToString:@"kd"]) {
        if (indexPath.row == self.searchArr.count) {//点击删除
            [NSUserDefualManager storageValue:@[] forKey:@"kd"];
            self.searchArr = [NSUserDefualManager getValueByKey:@"kd"];
            self.tableV.hidden = YES;
            [self.tableV reloadData];
        }else {
            self.keyWord = self.searchArr[indexPath.row];
            detailClientViewController *dCVC = [[detailClientViewController alloc]init];
            dCVC.key = self.keyWord;
            dCVC.isFrom = @"1";
            [self.navigationController pushViewController:dCVC animated:YES];
        }
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"搜索历史";
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 35;
}
-(void)initTopView {//创建头部搜索
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(10, 27, ScreenWidth - 70, 30)];
    searchView.backgroundColor =  [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0];
    searchView.layer.cornerRadius = 15;
    [self.navBar addSubview:searchView];
    UIImageView *seachImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 15, 15)];
    seachImg.image = [UIImage imageNamed:@"searchtop.png"];
    [searchView addSubview:seachImg];
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(30, 0, ScreenWidth - 100, 30)];
    self.textField.delegate = self;
    self.textField.tag = 100;
    self.textField.returnKeyType = UIReturnKeySearch;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    if ([self.isFrom isEqualToString:@"khgl"]) {
        self.textField.placeholder = @"请输入昵称、手机号或关键字";
    }else if ([self.isFrom isEqualToString:@"sxy"]) {
        self.textField.placeholder = @"请输入标题";
    }else if ([self.isFrom isEqualToString:@"kd"]) {
        self.textField.placeholder = @"请输入单号，手机号";
    }
    self.textField.font = [UIFont systemFontOfSize:15];
    [searchView addSubview:self.textField];
    UIButton *cantButton = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 50, 32, 40, 20)];
    [cantButton setTitle:@"取消" forState:UIControlStateNormal];
    cantButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cantButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cantButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];//取消按钮
    [self.navBar addSubview:cantButton];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.alpha = 0.2;
    lineV.backgroundColor = [UIColor lightGrayColor];
    [self.navBar addSubview:lineV];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {//点击搜索按钮事件
    [textField resignFirstResponder];
    if (textField.tag == 100) {
        if (textField.text.length == 0) {
            [[MessShowView share] showWithMess:@"搜索内容不能为空"];
        }
        if (self.textField.text.length != 0) {
            NSString *changedStr = [self.textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
            if ([self.isFrom isEqualToString:@"khgl"]) {
                if (changedStr.length != 0) {
                    [NSUserDefualManager addNewRecord:self.textField.text forKey:@"khgl"];
                }
                self.keyWord = textField.text;
                detailClientViewController *dCVC = [[detailClientViewController alloc]init];
                dCVC.key = self.keyWord;
                dCVC.isFrom = @"0";
                [self.navigationController pushViewController:dCVC animated:YES];
            }else if ([self.isFrom isEqualToString:@"sxy"]) {
                if (changedStr.length != 0) {
                    [NSUserDefualManager addNewRecord:self.textField.text forKey:@"sxy"];
                }
                self.keyWord = textField.text;
                BusinessSchollController *busScVC = [[BusinessSchollController alloc]init];
                busScVC.keyword = self.keyWord;
                [self.navigationController pushViewController:busScVC animated:YES];
            }else if ([self.isFrom isEqualToString:@"kd"]) {
                if (changedStr.length != 0) {
                    [NSUserDefualManager addNewRecord:self.textField.text forKey:@"kd"];
                }
                self.keyWord = textField.text;
                detailClientViewController *dCVC = [[detailClientViewController alloc]init];
                dCVC.key = self.keyWord;
                dCVC.isFrom = @"1";
                [self.navigationController pushViewController:dCVC animated:YES];
            }
        }
    }
    return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField {//结束编辑事件
    [textField resignFirstResponder];
    if (textField.tag == 100) {
        NSLog(@"%@",textField.text);
    }
}
-(void)cancelAction {//取消按钮
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
